home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / ctutor2.zip / CHARCLAS.C < prev    next >
C/C++ Source or Header  |  1987-07-04  |  1KB  |  44 lines

  1.                                          /* Chapter 13 - Program 2 */
  2. #include "stdio.h"
  3. #include "ctype.h"       /* Note - your compiler may not need this */
  4.  
  5. main()
  6. {
  7. FILE *fp;
  8. char line[80], filename[24];
  9. char *c;
  10.  
  11.    printf("Enter filename -> ");
  12.    scanf("%s",filename);
  13.    fp = fopen(filename,"r");
  14.  
  15.    do {
  16.       c = fgets(line,80,fp);   /* get a line of text */
  17.       if (c != NULL) {
  18.          count_the_data(line);
  19.       }
  20.    } while (c != NULL);
  21.  
  22.    fclose(fp);
  23. }
  24.  
  25. count_the_data(line)
  26. char line[];
  27. {
  28. int whites, chars, digits;
  29. int index;
  30.  
  31.    whites = chars = digits = 0;
  32.  
  33.    for (index = 0;line[index] != 0;index++) {
  34.       if (isalpha(line[index]))   /* 1 if line[] is alphabetic  */
  35.           chars++;
  36.       if (isdigit(line[index]))   /* 1 if line[] is a digit     */
  37.           digits++;
  38.       if (isspace(line[index]))   /* 1 if line[] is blank, tab, */
  39.           whites++;               /*           or newline       */
  40.    }   /* end of counting loop */
  41.  
  42.    printf("%3d%3d%3d %s",whites,chars,digits,line);
  43. }
  44.